<template>
  <div>
    <MyTable :arr='list'>
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body='scope'>
        <td>{{ scope.row.id }}</td>
        <td>{{ scope.row.goods_name }}</td>
        <td>{{ scope.row.goods_price }}</td>
        <td>
          <input
            class="tag-input form-control"
            type="text"
            style="width:100px"
            v-if="scope.row.inputVisible"
            v-focus
            @blur="scope.row.inputVisible = false"
            @keydown.enter="enterFn(scope.row)"
            v-model="scope.row.inputValue"
            @keydown.esc="scope.row.inputValue = ''"
          />
          <button
            v-else
            style="display:block"
            class="btn btn-primary btn-sm add-tag"
            @click="scope.row.inputVisible =true"
          >+添加标签。</button>
          <span
            v-for="(str,ind) in scope.row.tags"
            :key='ind'
            class="badge badge-warning"
          >
            {{ str }}
          </span>
        </td>
        <td><button
            class="btn btn-danger btn-sm"
            @click="removeBtn(scope.index)"
          >删除。</button></td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from '../components/MyTable'
import axios from 'axios'
axios.defaults.baseURL = "https://www.escook.cn"
export default {
  data () {
    return {
      list: []
    }
  },
  components: {
    MyTable
  },
  created () {
    axios({
      url: '/api/goods'
    }).then(res => {
      this.list = res.data.data
    })
  },
  methods: {
    removeBtn (index) {
      this.list.splice(index, 1)
    },
    enterFn (obj) {
      if (obj.inputValue.trim().length === 0) {
        alert('请输入添加的标签。')
        return
      }
      obj.tags.push(obj.inputValue)
      obj.inputValue = ''
    }
  }

}
</script>

<style>
</style>